{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b25dc9fb",
   "metadata": {},
   "source": [
    "\n",
    "* 时间：2022-11-22/week14\n",
    "* 内容：函数与模块\n",
    "-----------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "465c4b41",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello.\n"
     ]
    }
   ],
   "source": [
    "def greet_user(): #定义一个函数，关键词为\"def”，函数名为“greet_user”，最后以“:”结尾\n",
    "    '''显示最简单的问候语函数'''  #描述函数的具体功能\n",
    "    print(\"Hello.\")             #函数体的代码块，用书实现函数功能\n",
    "    \n",
    "greet_user()               #调用函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "06768f92",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello V\n"
     ]
    }
   ],
   "source": [
    "def greet_user(username):\n",
    "    \"\"\"显示最简单的问候语函数\"\"\"\n",
    "    print(\"Hello\",username)\n",
    "greet_user(\"V\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "722e111d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数实践：\n",
    "def NFU_NEWS_URL(分类,页码):\n",
    "    \"\"\"可以快速查询广州南方学院新闻的页面链接\n",
    "    分类：[\"xxyw\",\"xydt\",\"tzgg\",\"ztb\",\"gxtd\"]\n",
    "    页码:[113,106,37,27,29]\n",
    "    \"\"\"\n",
    "    for i in range(页码):\n",
    "        print(\"https://www.nfu.edu.cn/\"+分类+\"/index\"+str(i)+\".htm\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "87fbb835",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function NFU_NEWS_URL in module __main__:\n",
      "\n",
      "NFU_NEWS_URL(分类, 页码)\n",
      "    可以快速查询广州南方学院新闻的页面链接\n",
      "    分类：[\"xxyw\",\"xydt\",\"tzgg\",\"ztb\",\"gxtd\"]\n",
      "    页码:[113,106,37,27,29]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help( NFU_NEWS_URL)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "6e8362b1",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://www.nfu.edu.cn/xydt/index0.htm\n",
      "https://www.nfu.edu.cn/xydt/index1.htm\n",
      "https://www.nfu.edu.cn/xydt/index2.htm\n",
      "https://www.nfu.edu.cn/xydt/index3.htm\n",
      "https://www.nfu.edu.cn/xydt/index4.htm\n",
      "https://www.nfu.edu.cn/xydt/index5.htm\n",
      "https://www.nfu.edu.cn/xydt/index6.htm\n",
      "https://www.nfu.edu.cn/xydt/index7.htm\n",
      "https://www.nfu.edu.cn/xydt/index8.htm\n",
      "https://www.nfu.edu.cn/xydt/index9.htm\n",
      "https://www.nfu.edu.cn/xydt/index10.htm\n",
      "https://www.nfu.edu.cn/xydt/index11.htm\n",
      "https://www.nfu.edu.cn/xydt/index12.htm\n",
      "https://www.nfu.edu.cn/xydt/index13.htm\n",
      "https://www.nfu.edu.cn/xydt/index14.htm\n",
      "https://www.nfu.edu.cn/xydt/index15.htm\n",
      "https://www.nfu.edu.cn/xydt/index16.htm\n",
      "https://www.nfu.edu.cn/xydt/index17.htm\n",
      "https://www.nfu.edu.cn/xydt/index18.htm\n",
      "https://www.nfu.edu.cn/xydt/index19.htm\n",
      "https://www.nfu.edu.cn/xydt/index20.htm\n",
      "https://www.nfu.edu.cn/xydt/index21.htm\n",
      "https://www.nfu.edu.cn/xydt/index22.htm\n",
      "https://www.nfu.edu.cn/xydt/index23.htm\n",
      "https://www.nfu.edu.cn/xydt/index24.htm\n",
      "https://www.nfu.edu.cn/xydt/index25.htm\n",
      "https://www.nfu.edu.cn/xydt/index26.htm\n",
      "https://www.nfu.edu.cn/xydt/index27.htm\n",
      "https://www.nfu.edu.cn/xydt/index28.htm\n",
      "https://www.nfu.edu.cn/xydt/index29.htm\n",
      "https://www.nfu.edu.cn/xydt/index30.htm\n",
      "https://www.nfu.edu.cn/xydt/index31.htm\n",
      "https://www.nfu.edu.cn/xydt/index32.htm\n",
      "https://www.nfu.edu.cn/xydt/index33.htm\n",
      "https://www.nfu.edu.cn/xydt/index34.htm\n",
      "https://www.nfu.edu.cn/xydt/index35.htm\n",
      "https://www.nfu.edu.cn/xydt/index36.htm\n",
      "https://www.nfu.edu.cn/xydt/index37.htm\n",
      "https://www.nfu.edu.cn/xydt/index38.htm\n",
      "https://www.nfu.edu.cn/xydt/index39.htm\n",
      "https://www.nfu.edu.cn/xydt/index40.htm\n",
      "https://www.nfu.edu.cn/xydt/index41.htm\n",
      "https://www.nfu.edu.cn/xydt/index42.htm\n",
      "https://www.nfu.edu.cn/xydt/index43.htm\n",
      "https://www.nfu.edu.cn/xydt/index44.htm\n",
      "https://www.nfu.edu.cn/xydt/index45.htm\n",
      "https://www.nfu.edu.cn/xydt/index46.htm\n",
      "https://www.nfu.edu.cn/xydt/index47.htm\n",
      "https://www.nfu.edu.cn/xydt/index48.htm\n",
      "https://www.nfu.edu.cn/xydt/index49.htm\n",
      "https://www.nfu.edu.cn/xydt/index50.htm\n",
      "https://www.nfu.edu.cn/xydt/index51.htm\n",
      "https://www.nfu.edu.cn/xydt/index52.htm\n",
      "https://www.nfu.edu.cn/xydt/index53.htm\n",
      "https://www.nfu.edu.cn/xydt/index54.htm\n",
      "https://www.nfu.edu.cn/xydt/index55.htm\n",
      "https://www.nfu.edu.cn/xydt/index56.htm\n",
      "https://www.nfu.edu.cn/xydt/index57.htm\n",
      "https://www.nfu.edu.cn/xydt/index58.htm\n",
      "https://www.nfu.edu.cn/xydt/index59.htm\n",
      "https://www.nfu.edu.cn/xydt/index60.htm\n",
      "https://www.nfu.edu.cn/xydt/index61.htm\n",
      "https://www.nfu.edu.cn/xydt/index62.htm\n",
      "https://www.nfu.edu.cn/xydt/index63.htm\n",
      "https://www.nfu.edu.cn/xydt/index64.htm\n",
      "https://www.nfu.edu.cn/xydt/index65.htm\n",
      "https://www.nfu.edu.cn/xydt/index66.htm\n",
      "https://www.nfu.edu.cn/xydt/index67.htm\n",
      "https://www.nfu.edu.cn/xydt/index68.htm\n",
      "https://www.nfu.edu.cn/xydt/index69.htm\n",
      "https://www.nfu.edu.cn/xydt/index70.htm\n",
      "https://www.nfu.edu.cn/xydt/index71.htm\n",
      "https://www.nfu.edu.cn/xydt/index72.htm\n",
      "https://www.nfu.edu.cn/xydt/index73.htm\n",
      "https://www.nfu.edu.cn/xydt/index74.htm\n",
      "https://www.nfu.edu.cn/xydt/index75.htm\n",
      "https://www.nfu.edu.cn/xydt/index76.htm\n",
      "https://www.nfu.edu.cn/xydt/index77.htm\n",
      "https://www.nfu.edu.cn/xydt/index78.htm\n",
      "https://www.nfu.edu.cn/xydt/index79.htm\n",
      "https://www.nfu.edu.cn/xydt/index80.htm\n",
      "https://www.nfu.edu.cn/xydt/index81.htm\n",
      "https://www.nfu.edu.cn/xydt/index82.htm\n",
      "https://www.nfu.edu.cn/xydt/index83.htm\n",
      "https://www.nfu.edu.cn/xydt/index84.htm\n",
      "https://www.nfu.edu.cn/xydt/index85.htm\n",
      "https://www.nfu.edu.cn/xydt/index86.htm\n",
      "https://www.nfu.edu.cn/xydt/index87.htm\n",
      "https://www.nfu.edu.cn/xydt/index88.htm\n",
      "https://www.nfu.edu.cn/xydt/index89.htm\n",
      "https://www.nfu.edu.cn/xydt/index90.htm\n",
      "https://www.nfu.edu.cn/xydt/index91.htm\n",
      "https://www.nfu.edu.cn/xydt/index92.htm\n",
      "https://www.nfu.edu.cn/xydt/index93.htm\n",
      "https://www.nfu.edu.cn/xydt/index94.htm\n",
      "https://www.nfu.edu.cn/xydt/index95.htm\n",
      "https://www.nfu.edu.cn/xydt/index96.htm\n",
      "https://www.nfu.edu.cn/xydt/index97.htm\n",
      "https://www.nfu.edu.cn/xydt/index98.htm\n",
      "https://www.nfu.edu.cn/xydt/index99.htm\n",
      "https://www.nfu.edu.cn/xydt/index100.htm\n",
      "https://www.nfu.edu.cn/xydt/index101.htm\n",
      "https://www.nfu.edu.cn/xydt/index102.htm\n",
      "https://www.nfu.edu.cn/xydt/index103.htm\n",
      "https://www.nfu.edu.cn/xydt/index104.htm\n",
      "https://www.nfu.edu.cn/xydt/index105.htm\n"
     ]
    }
   ],
   "source": [
    "NFU_NEWS_URL(\"xydt\",106)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "3c2e5e03",
   "metadata": {},
   "outputs": [],
   "source": [
    "import nfu_crawl #第23行代码可以删除 在这之前已在目录文件中保存过数据，以.py形式保存"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "b711a907",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function NFU_NEWS_URL in module nfu_crawl:\n",
      "\n",
      "NFU_NEWS_URL(分类, 页码)\n",
      "    可以快速查询广州南方学院新闻的页面链接\n",
      "    分类：[\"xxyw\",\"xydt\",\"tzgg\",\"ztb\",\"gxtd\"]\n",
      "    页码:[113,106,37,27,29]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(nfu_crawl.NFU_NEWS_URL)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf793110",
   "metadata": {},
   "outputs": [],
   "source": [
    "def NFU_NEWS_URL(分类,页码):\n",
    "    \"\"\"可以快速查询广州南方学院新闻的页面链接\n",
    "    分类：[\"xxyw\",\"xydt\",\"tzgg\",\"ztb\",\"gxtd\"]\n",
    "    页码:[113,106,37,27,29]\n",
    "    \"\"\"\n",
    "    for i in range(页码):\n",
    "        print(\"http://i.chaoxing.com/\"+分类+\"/index\"+str(i)+\".htm\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "cc01d6ff",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (1676892700.py, line 6)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [29]\u001b[1;36m\u001b[0m\n\u001b[1;33m    func_text(x=1,2)\u001b[0m\n\u001b[1;37m                   ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "#egl\n",
    "def func_text(x,y):\n",
    "    print(x)\n",
    "    print(y)\n",
    "\n",
    "func_text(x=1,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "908fa620",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "def func_text(x,y):\n",
    "    print(x)\n",
    "    print(y)\n",
    "func_text(1,y=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "c874852c",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (4281668655.py, line 4)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [31]\u001b[1;36m\u001b[0m\n\u001b[1;33m    func_text(1,y=2,3)\u001b[0m\n\u001b[1;37m                     ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "def func_text(x,y,z):\n",
    "    print(x)\n",
    "    print(y)\n",
    "func_text(1,y=2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "1fe61688",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "def func_text(x,y,z):\n",
    "    print(x)\n",
    "    print(y)\n",
    "func_text(1,2,z=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "6e88f508",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a hamster.\n",
      "My hamster's name is Harry.\n",
      "\n",
      "I have a cat.\n",
      "My cat's name is Python.\n"
     ]
    }
   ],
   "source": [
    "def describe_pet(pet_name,animal_type=\"dog\",):\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI have a \" + animal_type + \".\")\n",
    "    print(\"My \" + animal_type + \"'s name is \"+pet_name.title() + \".\")\n",
    "\n",
    "describe_pet('harry','hamster')\n",
    "describe_pet('python','cat')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f43ffe27",
   "metadata": {},
   "source": [
    "## 3.函数的返回值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6fcecba0",
   "metadata": {},
   "source": [
    "* 对计算机而言:函数必须有返回值（公理）\n",
    "* python：返回值给了一个默认值（none）,也应该有一个返回值\n",
    "> 1.返回值的第一种情况：返回函数过程的计算结果      \n",
    "> 2.返回值的第二种情况：返回函数过程运算过程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "b1bb4c36",
   "metadata": {},
   "outputs": [],
   "source": [
    "def func(x):\n",
    "    \"\"\"计算y=2x\"\"\"\n",
    "    y=2*x\n",
    "    return y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "f9cbc15c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "print(func(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd885e5e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
